//
//  OBMomentContentCell.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/3.
//  Copyright © 2018年 obally. All rights reserved.
//

#import "OBMomentContentCell.h"
#import "OBMomentCommentItemViewModel.h"

@interface OBMomentContentCell ()
/// 正文
@property (nonatomic, readwrite, weak) YYLabel *contentLable;

@property (nonatomic , readwrite , strong) OBMomentCommentItemViewModel *viewModel;
@end
@implementation OBMomentContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"MomentContentCell";
    OBMomentContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        // 初始化
        [self _setup];
        
        // 创建自控制器
        [self _setupSubViews];
    }
    return self;
}
- (void)bindViewModel:(OBMomentCommentItemViewModel *)viewModel
{
    self.viewModel = viewModel;
    /// 文本
    self.contentLable.textLayout = viewModel.contentLableLayout;
    self.contentLable.frame = viewModel.contentLableFrame;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.divider.hidden = YES;
}
#pragma mark - 初始化
- (void)_setup
{
    // 设置颜色
    self.contentView.backgroundColor = OBMomentCommentViewBackgroundColor;
}
#pragma mark - 创建自控制器
- (void)_setupSubViews{
    /// 正文
    YYLabel *contentLable = [[YYLabel alloc] init];
    contentLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向顶部对齐
    contentLable.textVerticalAlignment = YYTextVerticalAlignmentTop;
    /// 异步渲染和布局
    contentLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    contentLable.ignoreCommonProperties = YES;
    contentLable.fadeOnAsynchronouslyDisplay = NO;
    contentLable.fadeOnHighlight = NO;
    contentLable.preferredMaxLayoutWidth = OBMomentCommentViewWidth()-2*OBMomentCommentViewContentLeftOrRightInset;
    [self.contentView addSubview:contentLable];
    self.contentLable = contentLable;
    
    /// 分割线
    UIImageView *divider = [[UIImageView alloc] initWithImage:OBImageNamed(@"wx_albumCommentHorizontalLine_33x1")];
    divider.backgroundColor = OBGlobalBottomLineColor;
    self.divider = divider;
    [self.contentView addSubview:divider];

    contentLable.highlightLongPressAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@"高亮长按label事件");
    };
}


#pragma mark - Override
/// PS:重写cell的尺寸 这是评论View关键
- (void)setFrame:(CGRect)frame{
    frame.origin.x = OBMomentContentLeftOrRightInset+OBMomentAvatarWH+OBMomentContentInnerMargin;
    frame.size.width = OBMomentCommentViewWidth();
    [super setFrame:frame];
}

#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    self.divider.frame =CGRectMake(0, self.mh_height-OBGlobalBottomLineHeight, self.mh_width, OBGlobalBottomLineHeight);
}

@end
